---
sidebar_label: Anthropic
---

# ChatAnthropic

LangChain supports Anthropic's Claude family of chat models.

You'll first need to install the [`@langchain/anthropic`](https://www.npmjs.com/package/@langchain/anthropic) package:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/anthropic
```

You'll also need to sign up and obtain an [Anthropic API key](https://www.anthropic.com/).
Set it as an environment variable named `ANTHROPIC_API_KEY`, or pass it into the constructor as shown below.

## Usage

import UnifiedModelParamsTooltip from "@mdx_components/unified_model_params_tooltip.mdx";

<UnifiedModelParamsTooltip></UnifiedModelParamsTooltip>

You can initialize an instance like this:

import CodeBlock from "@theme/CodeBlock";
import Anthropic from "@examples/models/chat/integration_anthropic.ts";

<CodeBlock language="typescript">{Anthropic}</CodeBlock>

## Multimodal inputs

Claude-3 models support image multimodal inputs. The passed input must be a base64 encoded image with the
filetype as a prefix (e.g. `data:image/png;base64,{YOUR_BASE64_ENCODED_DATA}`).
Here's an example:

import AnthropicMultimodal from "@examples/models/chat/integration_anthropic_multimodal.ts";

<CodeBlock language="typescript">{AnthropicMultimodal}</CodeBlock>

See [the official docs](https://docs.anthropic.com/claude/docs/vision#what-image-file-types-does-claude-support)
for a complete list of supported file types.

## Agents

Anthropic models that support tool calling can be used in the Tool Calling agent. Here's an example:

import AnthropicToolCallingAgent from "@examples/models/chat/integration_anthropic_tool_calling_agent.ts";

<CodeBlock language="typescript">{AnthropicToolCallingAgent}</CodeBlock>

:::tip
See the LangSmith trace [here](https://smith.langchain.com/public/e93ff7f6-03f7-4eb1-96c8-09a17dee1462/r)
:::

## Custom headers

You can pass custom headers in your requests like this:

import AnthropicCustomHeaders from "@examples/models/chat/integration_anthropic_custom_headers.ts";

<CodeBlock language="typescript">{AnthropicCustomHeaders}</CodeBlock>

## Tools

The Anthropic API supports tool calling, along with multi-tool calling. The following examples demonstrate how to call tools:

### Single Tool

import AnthropicSingleTool from "@examples/models/chat/integration_anthropic_single_tool.ts";

<CodeBlock language="typescript">{AnthropicSingleTool}</CodeBlock>

:::tip
See the LangSmith trace [here](https://smith.langchain.com/public/90c03ed0-154b-4a50-afbf-83dcbf302647/r)
:::

### Forced tool calling

import AnthropicForcedTool from "@examples/models/chat/integration_anthropic_forced_tool.ts";

In this example we'll provide the model with two tools:

- `calculator`
- `get_weather`

Then, when we call `bindTools`, we'll force the model to use the `get_weather` tool by passing the `tool_choice` arg like this:

```typescript
.bindTools({
  tools,
  tool_choice: {
    type: "tool",
    name: "get_weather",
  }
});
```

Finally, we'll invoke the model, but instead of asking about the weather, we'll ask it to do some math.
Since we explicitly forced the model to use the `get_weather` tool, it will ignore the input and return the weather information (in this case it returned `<UNKNOWN>`, which is expected.)

<CodeBlock language="typescript">{AnthropicForcedTool}</CodeBlock>

The `bind_tools` argument has three possible values:

- `{ type: "tool", name: "tool_name" }` - Forces the model to use the specified tool.
- `"any"` - Allows the model to choose the tool, but still forcing it to choose at least one.
- `"auto"` - The default value. Allows the model to select any tool, or none.

:::tip
See the LangSmith trace [here](https://smith.langchain.com/public/c5cc8fe7-5e76-4607-8c43-1e0b30e4f5ca/r)
:::

### `withStructuredOutput`

import AnthropicWSA from "@examples/models/chat/integration_anthropic_wsa.ts";

<CodeBlock language="typescript">{AnthropicWSA}</CodeBlock>

:::tip
See the LangSmith trace [here](https://smith.langchain.com/public/efbd11c5-886e-4e07-be1a-951690fa8a27/r)
:::
